<template>
	<view class="usersetup">
		<!-- #ifdef MP-WEIXIN -->
		<button open-type="chooseAvatar" @chooseavatar="chooseavatar" class="ys-li">
			<text>头像</text>
			<view class="name">
				<image :src="avatarUrl || siteConf.logo" class="avatar" mode="" @click.stop="preview"></image>
				<image src="/static/index/jiantou.png" mode=""></image>
			</view>
		</button>
		<!-- #endif -->

		<!-- #ifndef MP-WEIXIN -->
		<view class="ys-li" @click="changeImg">
			<text>头像</text>
			<view class="name">
				<image :src="avatarUrl || siteConf.logo" mode="" class="avatar" @click.stop="preview"></image>
				<image src="/static/index/jiantou.png" mode=""></image>
			</view>
		</view>
		<!-- #endif -->

		<navigator url="/pages/userinfo/index">
			<view class="ys-li">
				<text>昵称</text>
				<view class="name">
					{{info.nickname}}
					<image src="/static/index/jiantou.png" mode=""></image>
				</view>

			</view>
		</navigator>

		<view>
			<picker mode="multiSelector" :range="regionArray" @change="changeRegion" @columnchange="changeRegionColumn">
				<view class="ys-li">
					<text>地区</text>
					<view class="name">
						{{info.region}}
						<image src="/static/index/jiantou.png" mode=""></image>
					</view>
				</view>
			</picker>

		</view>
		<navigator url="/pages/userpassword/index">
			<view class="ys-li">
				<text>登录密码</text>
				<image src="/static/index/jiantou.png" mode=""></image>
			</view>
		</navigator>
		<navigator url="/pages/userpasspay/index">
			<view class="ys-li">
				<text>支付密码</text>
				<image src="/static/index/jiantou.png" mode=""></image>
			</view>
		</navigator>
	</view>
</template>

<script>
	import {
		UserInfo,
		changeNickName,
		getArea
	} from "@/api/api.js"
	import {
		saveUserRegion
	} from "@/api/user.js"
	export default {
		data() {
			return {
				regionList: [],
				regionArray: [],
				provinceArray: [],
				cityArray: [],
				values: [0, 0],
				info: [],
				avatarUrl: ''
			}
		},
		onLoad() {
			this.getArea()
			this.UserInfo()
		},
		methods: {
			changeRegion(e) {
				console.log(e);
				this.values = e.detail.value
				let province = this.regionList[this.values[0]].id
				let p_name = this.regionList[this.values[0]].name
				let city = this.regionList[this.values[0]].children[this.values[1]].id
				let c_name = this.regionList[this.values[0]].children[this.values[1]].name
				saveUserRegion({
					province: province,
					city: city
				}).then(res => {
					if (res.code == 0) {
						this.info.region = `${p_name}-${c_name}`
					} else {
						return uni.$showMsg(res.msg)
					}
				})
			},
			changeRegionColumn(e) {
				let column = e.detail.column
				let value = e.detail.value
				if (column == 0) {
					this.cityArray = this.regionList[value].children.map(item => item.name)
					this.regionArray = [this.provinceArray, this.cityArray]
				}
			},
			UserInfo() {
				UserInfo().then(res => {
					if (res.code == 0) {
						this.info = res.data.user
						if (!res.data.user) {
							uni.switchTab({
								url: '/pages/user/index'
							})
							return false
						}
						this.name = this.info.nickname
						this.avatarUrl = res.data.user.avatar_path
					}
				})
			},
			getArea() {
				getArea({
					layer: 2
				}).then(res => {
					if (res.code == 0) {
						this.regionList = res.data
						this.provinceArray = this.regionList.map(item => {
							return item.name
						})
						this.cityArray = this.regionList[0].children.map(item => {
							return item.name
						})
						this.regionArray = [this.provinceArray, this.cityArray]
					}
				})
			},
			preview() {
				uni.previewImage({
					urls: [this.avatarUrl]
				})
			},
			chooseavatar(e) {
				var that = this
				uni.uploadFile({
					url: that.$config.requestUrl +
						'/common/upload',
					header: {
						'tokens': uni.getStorageSync('tokens')
					},
					formData: {
						accept: 'image',
						is_avatar: 1
					},
					filePath: e.detail.avatarUrl,
					name: 'UploadFile',
					success: (res) => {
						uni.hideLoading()
						let imgRes = JSON.parse(res.data); //微信和头条支持
						if (imgRes.code == 0) {
							let imgData = imgRes.data
							that.avatarUrl = imgData.path
						} else {
							uni.$showMsg(imgRes.msg)
							return
						}
					},
					fail(err) {
						uni.hideLoading()
					}
				})
			},
			changeImg() {
				let that = this
				uni.chooseImage({
					sourceType: ['album', 'camera'], //从相册选择
					count: 1,
					success: (res) => {
						let igmFile = res.tempFilePaths;
						uni.showLoading({
							mask: true,
							title: '加载中'
						})
						uni.uploadFile({
							url: that.$config.requestUrl +
								'/common/upload',
							header: {
								'tokens': uni.getStorageSync('tokens')
							},
							formData: {
								accept: 'image',
								is_avatar: 1
							},
							filePath: igmFile[0],
							name: 'UploadFile',
							success: (res) => {
								uni.hideLoading()
								let imgRes = JSON.parse(res.data); //微信和头条支持
								if (imgRes.code == 0) {
									let imgData = imgRes.data
									that.avatarUrl = imgData.path
								} else {
									uni.$showMsg(imgRes.msg)
									return
								}
							},
							fail(err) {
								uni.hideLoading()
							}
						})
					},
				});
			},
		}
	}
</script>

<style lang="scss">
	.usersetup {}

	button {
		line-height: 0;

		&::after {
			border: 0;
		}
	}

	.ys-li {
		font-size: 14px;
		color: #777;
		display: flex;
		background: #FFFFFF;
		justify-content: space-between;
		margin: 0 auto;
		border-bottom: 1px solid #efefef;
		padding: 40rpx;
		align-items: center;

		image {
			width: 30rpx;
			height: 30rpx;
			margin-left: 5px;
		}

		.avatar {
			width: 50px;
			height: 50px;
			border-radius: 4px;
			position: relative;
			z-index: 999;
		}

		.name {
			display: flex;
			align-items: center;

		}
	}
</style>